package com.zhongwang.business.module.baseCostManage.listener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.zhongwang.business.common.ImportCallBack;
import com.zhongwang.business.gen.entity.DictCommon;
import com.zhongwang.business.gen.entity.User;
import com.zhongwang.business.module.baseCostManage.dao.InputImportCostDao;
import com.zhongwang.business.module.baseCostManage.model.DictCommonModel;
import com.zhongwang.business.module.baseCostManage.model.InputOutsourcingModel;
import com.zhongwang.business.module.baseCostManage.model.InputRolledSteelPlanModel;
import com.zhongwang.business.util.ShiroUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 *  原料计划数据导入 listener
 * @author kxx
 * @date 2020年10月26日11:09
 */
public class InputRolledSteelPlanListener extends AnalysisEventListener<InputRolledSteelPlanModel> {


    private InputImportCostDao inputImportCostDao;
    private ImportCallBack callBack;
    private List<InputRolledSteelPlanModel> inputRolledSteelPlanModel = new ArrayList<>();
    private List<DictCommonModel> dictCommon;
    private List<Integer> exceptionList = new ArrayList<>();
    private int i = 2;
    private InputRolledSteelPlanModel rolledSteelPlan;

    public InputRolledSteelPlanListener(InputRolledSteelPlanModel rolledSteelPlan, InputImportCostDao inputImportCostDao,
                                        ImportCallBack callBack,
                                        List<DictCommonModel> dictCommon) {
        this.inputImportCostDao = inputImportCostDao;
        this.callBack = callBack;
        this.dictCommon = dictCommon;
        this.rolledSteelPlan = rolledSteelPlan;
    }


    @Override
    public void invoke(InputRolledSteelPlanModel data, AnalysisContext context) {

//        DictCommonModel Type = getName(data.getOutAssistType());
        data.setPartName(rolledSteelPlan.getInputPartName());
        data.setKind(rolledSteelPlan.getInputKind());

        if (data.getOutMalDesc() == null || data.getOutMalDesc().trim().equals("")) {
            data.setReason(data.getReason() + "物料描述没填");
        }

        if (data.getOutTexture() == null || data.getOutTexture().trim().equals("")) {
            data.setReason(data.getReason() + "材质没填");
        } else if (getName(data.getOutTexture()) == null){
            data.setReason(data.getReason()+ "材质类型不存在");
        }

        if (data.getOutNum() == null) {
            data.setReason(data.getReason() + "数量没填");
        }
        else if (data.getOutNum() <= 0) {
            data.setReason(data.getReason() + "数量必须大于0");
        }
        if (data.getOutUnit() == null || data.getOutUnit().trim().equals("")) {
            data.setReason(data.getReason() + "单位没填");
        }
        if (inputImportCostDao.selectRolledSteelPlanRepetition(data) > 0) {
            data.setReason(data.getReason() + "重复数据");
        }
         /**判断并输出验证结果*/
        if (data.getReason()!= null && data.getReason().length() > 0) {
            inputRolledSteelPlanModel.add(data);
            i++;
            return;
        }
        User user = ShiroUtils.getUser();
        data.setInputUser(user.getName());
        data.setEditDate(new Date());
        data.setFkId(rolledSteelPlan.getKbId());

        data.setDesignType(rolledSteelPlan.getInputDesignType());
        data.setFkInputId(rolledSteelPlan.getInputMacId());

        data.setCostKind(rolledSteelPlan.getInputCostKind());
        data.setFkCode(rolledSteelPlan.getInputCode());
        data.setIsReceive(0);
        data.setOutDeleteFlag(0);
        data.setOutReportType(3);
        inputImportCostDao.insertRolledSteelPlan(data);
        i++;
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext context) {
        if (exceptionList.size() > 0) {
            InputRolledSteelPlanModel model = new InputRolledSteelPlanModel();
            model.setOutMalDesc("第" + exceptionList.toString() + "行数据格式不正确");
            inputRolledSteelPlanModel.add(model);
        }
        if (inputRolledSteelPlanModel.size() > 0) {
            callBack.onWarning(inputRolledSteelPlanModel);
        } else {
            callBack.onSuccess();
        }
    }
    @Override
    public void onException(Exception exception, AnalysisContext context) {
        exception.printStackTrace();
        String err = exception.getMessage();
        exceptionList.add(i);
        i++;
        return;
    }

    private DictCommonModel getName(String dicCommonTypes) {
        for (DictCommonModel dictCommon : dictCommon ) {
            if (dictCommon.getName() !=null && dictCommon.getName().equals(dicCommonTypes)) {
                return dictCommon;
            }
        }
        return null;
    }
}
